package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author @fiber-cloud
 * @Date 2025/10/30 9:54
 */
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    //重定向发起的是一个get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先判定用户的登录状态
        // 如果用户还没登录，要求先登录
        // 如果已经登录了，则根据 会话 中的用户名，将其显示到页面上

        //能拿到——一已登录
        HttpSession session = req.getSession(false);
        if (session == null){
            System.out.println("用户未登录！");
            resp.sendRedirect("login.html");
            return;
        }

        String username = (String) session.getAttribute("username");

        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("欢迎 "+ username + "回来！");
    }
}
